﻿using FreeDream.Common;
using FreeDream.IService;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Service
{
    /// <summary>
    /// HTTP请求
    /// </summary>
    public class HttpClientService : IHttpClientService
    {
        private readonly ILogger<HttpClientService> _logger;

        public HttpClientService(ILogger<HttpClientService> logger)
        {
            _logger = logger;
        }

        public string HttpGet(string url, Dictionary<string, string> headers = null)
        {
            var result = string.Empty;
            try
            {
                _logger.LogInformation($"HttpGet请求:{url},{JsonConvert.SerializeObject(headers)}");

                result = HttpHelper.HttpGet(url, headers);

                _logger.LogInformation($"HttpGet返回:{url},{result}");
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"HttpGet异常:{url},{ex.Message},{ex.StackTrace}");
            }

            return result;
        }

        public async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
        {
            var result = string.Empty;
            try
            {
                _logger.LogInformation($"HttpGetAsync请求:{url},{JsonConvert.SerializeObject(headers)}");

                result = await HttpHelper.HttpGetAsync(url, headers);

                _logger.LogInformation($"HttpGetAsync返回:{url},{result}");
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"HttpGetAsync异常:{url},{ex.Message},{ex.StackTrace}");
            }

            return result;
        }

        public string HttpPost(string url, string postData = null, Dictionary<string, string> headers = null, string contentType = "application/json", int timeOut = 30)
        {
            var result = string.Empty;
            try
            {
                _logger.LogInformation($"HttpPost请求:{url},{JsonConvert.SerializeObject(headers)}");

                result = HttpHelper.HttpPost(url, postData, headers, contentType, timeOut);

                _logger.LogInformation($"HttpPost返回:{url},{result}");
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"HttpPost异常:{url},{ex.Message},{ex.StackTrace}");
            }

            return result;
        }

        public async Task<string> HttpPostAsync(string url, string postData = null, Dictionary<string, string> headers = null, string contentType = "application/json", int timeOut = 30)
        {
            var result = string.Empty;
            try
            {
                _logger.LogInformation($"HttpPostAsync请求:{url},{JsonConvert.SerializeObject(headers)}");

                result = await HttpHelper.HttpPostAsync(url, postData, headers, contentType, timeOut);

                _logger.LogInformation($"HttpPostAsync返回:{url},{result}");
            }
            catch (Exception ex)
            {
                _logger.LogInformation($"HttpPostAsync异常:{url},{ex.Message},{ex.StackTrace}");
            }

            return result;
        }
    }
}
